/*
 * GameWaitPanel.java
 *
 * Created on September 21, 2008, 4:32 PM
 */
package no.eirikb.bomberman.applet;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import no.eirikb.bomberman.shared.Game;
import no.eirikb.bomberman.shared.User;

/**
 *
 * @author  eirikb
 */
public class GameWaitPanel extends javax.swing.JPanel {

    private Bomberman bomberman;
    private Game game;

    /** Creates new form GameWaitPanel */
    public GameWaitPanel(Bomberman bomberman) {
        this.bomberman = bomberman;
        initComponents();
    }

    public void reset(Game game) {
        this.game = game;
        readyButton.setSelected(false);
        updatePlayerList();
    }

    public void updatePlayerList() {
        System.out.println("upadte playerlist lol");
        playerList.setListData(game.getUsers().values().toArray(new User[0]));
        playerLabel.setText("Players [" + game.getUsers().size() + "/" + game.getMax() + "]:");
    }

    public void updateGame(Game game) {
        System.out.println("Updatelol 2");
        this.game = game;
        updatePlayerList();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headingLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        playerList = new javax.swing.JList();
        quitButton = new javax.swing.JButton();
        readyButton = new javax.swing.JToggleButton();
        playerLabel = new javax.swing.JLabel();

        setBackground(java.awt.Color.cyan);

        headingLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        headingLabel.setText("Waiting for other players...");

        playerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        playerList.setEnabled(false);
        jScrollPane1.setViewportView(playerList);

        quitButton.setText("QUIT");
        quitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitButtonActionPerformed(evt);
            }
        });

        readyButton.setText("READY!");
        readyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                readyButtonActionPerformed(evt);
            }
        });

        playerLabel.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        playerLabel.setText("Players [x/x]:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(headingLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(quitButton)
                            .addComponent(readyButton))
                        .addContainerGap(134, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(playerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(369, 369, 369))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headingLabel)
                .addGap(18, 18, 18)
                .addComponent(playerLabel)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(readyButton)
                        .addGap(6, 6, 6)
                        .addComponent(quitButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitButtonActionPerformed
    try {
        bomberman.getServer().leaveGame(bomberman.getHash(), bomberman.getUser().getNick());
        bomberman.getUser().setGameName(null);
        bomberman.show("menu");
    } catch (RemoteException ex) {//GEN-LAST:event_quitButtonActionPerformed
        Logger.getLogger(GameWaitPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private void readyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readyButtonActionPerformed
    try {
        bomberman.getServer().setReady(bomberman.getHash(), bomberman.getUser().getNick(), readyButton.isSelected());
    } catch (RemoteException ex) {
        Logger.getLogger(GameWaitPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
}//GEN-LAST:event_readyButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel headingLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel playerLabel;
    private javax.swing.JList playerList;
    private javax.swing.JButton quitButton;
    private javax.swing.JToggleButton readyButton;
    // End of variables declaration//GEN-END:variables
}
